package org.melord.cap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.wb.swt.SWTRes;

public class StyleLabel extends StyledText {

	public StyleLabel(Composite parent) {
		super(parent, SWT.SINGLE | SWT.CENTER);
		init();
	}

	private void init() {
		setAlignment(SWT.CENTER);
		// setBackground(SWTRes.getColor(SWT.COLOR_WIDGET_BACKGROUND));
		setEnabled(false);
	}

	public void enableStyle(ControlWraper ctrl) {
		String ctx = ctrl.getControl().getContext();
		if (ctrl.getText() != null && ctx != null) {
			int start = ctrl.getText().indexOf(ctx);
			if (start > -1) {
				setStyleRange(getColorStyle(start, ctx.length(), SWTRes
						.getColor(SWT.COLOR_BLUE)));
			}
		}
	}

	private StyleRange getColorStyle(int start, int length, Color color) {
		StyleRange styleRange = new StyleRange(start, length, color, null);
		styleRange.fontStyle = SWT.BOLD;
		return styleRange;
	}
}